/**
 * 
 */
package com.roosterrobotics.replicant;

import com.roosterrobotics.replicant.provider.ReplicantData;
import com.roosterrobotics.replicant.provider.ReplicantDbAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * @author goyo
 *
 */
 
public class CommunityUpdate extends Activity {
	
	private EditText mCommunityName;
	private Long mCommunityId;


	/**
	 * 
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.community_edit);
		setTitle(R.string.community_edit);

		
		mCommunityName = (EditText) findViewById(R.id.name);
		Button acceptButton = (Button) findViewById(R.id.accept);
		Button cancelButton = (Button) findViewById(R.id.cancel);
		
		Bundle extras = getIntent().getExtras();
		mCommunityId = null;
		
		if (extras != null) {
			String name = extras.getString(ReplicantData.Communities.COMMUNITY_NAME_COLUMN_NAME);
			mCommunityId = extras.getLong(ReplicantData.Communities.COMMUNITY_ID_COLUMN_NAME);
			
			if (name != null) {
				mCommunityName.setText(name);
			}

		}
		
		acceptButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Bundle bundle = new Bundle();
				bundle.putString(ReplicantDbAdapter.COMMUNITY_NAME, mCommunityName.getText().toString());
				if (mCommunityId != null) {
					bundle.putLong(ReplicantDbAdapter.COMMUNITY_ID, mCommunityId);
				}
				
				Intent mIntent = new Intent();
				mIntent.putExtras(bundle);
				setResult(RESULT_OK, mIntent);
				finish();
			}
		});
		
		cancelButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent mIntent = new Intent();
				setResult(RESULT_CANCELED, mIntent);
				finish();
			}
		});
		
		
	}


}
